//
//  UserSeriesViewModel.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/27.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserSeriesViewModel.h"
#import <FMDB/FMDB.h>
#import "UserGroup.h"
#import "UserItem.h"
#import "NSDate+FormateString.h"

@interface UserSeriesViewModel ()
@property (nonatomic, strong) NSArray *cities;
@property (nonatomic, strong) NSArray *provinces;
@property (nonatomic, copy)   NSArray *nations;
@property (copy, nonatomic) NSString *databasePath;



/** 身高 */
@property (nonatomic, strong) NSArray <NSString *> *heights;
/** 体重 */
@property (nonatomic, strong) NSArray <NSString *> *weights;
/** 球龄 */
@property (nonatomic, strong) NSArray <NSString *> *tennisExperiences;
/** 网球等级 */
@property (nonatomic, strong) NSArray <NSString *> *tennisLevels;

@property (assign, nonatomic) BOOL isEn;

/** 网球线的规格 */
@property (nonatomic, strong) NSArray <NSString *> *tennisLineSizes;
/** 网球线的张力 */
@property (nonatomic, strong) NSArray <NSString *> *tennisLineTenssions;
/** 网球线的型号 */
@property (nonatomic, strong) NSArray <NSString *> *tennisLineBrands;

@end

@implementation UserSeriesViewModel


+ (instancetype)shareInstance {
    
    static UserSeriesViewModel *_instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[UserSeriesViewModel alloc] init];
    });
    
    return _instance;
}

- (instancetype)init {
    if (self = [super init]) {
        
        NSArray *languages = [NSLocale preferredLanguages];
        NSString *currentLanguage = [languages objectAtIndex:0];
        if ([currentLanguage rangeOfString:@"zh-Hans"].location != NSNotFound){
            _isEn = NO;
            _databasePath = [[NSBundle mainBundle] pathForResource:@"Location" ofType:@"db"];
            
        } else {
            _isEn = YES;
            _databasePath = [[NSBundle mainBundle] pathForResource:@"LocationEn" ofType:@"db"];
        }
        
        self.provinces = [self provincesForCountryId:0];
        
    }
    
    return self;
}

- (NSArray *)citiesWithProvinces:(NSString *)provinceName {
    
    NSString *provinceId = [self proviceIdByProviceName:provinceName];
    
    _cities = [self citiesForProvinceId:[provinceId integerValue]];
    
    return _cities;
    
}

- (NSArray *)provinces {
    
    _provinces = [self provincesForCountryId:0];
    
    return _provinces;
}

- (NSArray *)provincesForCountryId:(NSInteger)countryId{
    FMDatabase *db = [FMDatabase databaseWithPath:_databasePath];
    NSMutableArray *provinces = [@[] mutableCopy];
    if ([db open]) {
        FMResultSet *resultSet;
        if (_isEn) {
            resultSet = [db executeQuery:@"SELECT * FROM province_en where CountryID = ?",[@(countryId) stringValue]];
        } else {
            resultSet = [db executeQuery:@"SELECT * FROM province where CountryID = ?",[@(countryId) stringValue]];
        }
        while ([resultSet next]) {
            NSString *provinceName = [resultSet stringForColumn:@"Name"];
            [provinces addObject:provinceName];
        }
        
        [db close];
    }
    
    return [provinces copy];
}

- (NSArray *)nations{
    if (!_nations) {
        
        FMDatabase *db = [FMDatabase databaseWithPath:_databasePath];
        NSMutableArray *countries = [@[] mutableCopy];
        if ([db open]) {
            FMResultSet *resultSet;
            if (_isEn) {
                resultSet = [db executeQuery:@"SELECT * FROM country_en"];
            } else {
                resultSet = [db executeQuery:@"SELECT * FROM country"];
            }
            while ([resultSet next]) {
                NSString *countryName = [resultSet stringForColumn:@"Name"];
                [countries addObject:countryName];
            }
        }
        _nations = [countries copy];
    }
    return _nations;
}

- (NSString *)proviceIdByProviceName:(NSString *)proviceName{
    FMDatabase *db = [FMDatabase databaseWithPath:_databasePath];
    NSString *provinceId;
    if ([db open]) {
        FMResultSet *resultSet;
        if (_isEn) {
            resultSet = [db executeQuery:@"SELECT ID FROM province_en where Name = ?", proviceName];
        } else {
            resultSet = [db executeQuery:@"SELECT ID FROM province where Name = ?", proviceName];
        }
        while ([resultSet next]) {
            provinceId =  [resultSet stringForColumn:@"ID"];
            NSLog(@"provinceId = %@",provinceId);
        }
        
        [db close];
    }
    
    return provinceId;
}

- (NSArray *)citiesForProvinceId:(NSInteger)provinceId
{
    //    NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"Location" ofType:@"db"];
    FMDatabase *db = [FMDatabase databaseWithPath:_databasePath];
    NSMutableArray *cities = [@[] mutableCopy];
    if ([db open]) {
        FMResultSet *resultSet;
        if (_isEn) {
            resultSet = [db executeQuery:@"SELECT * FROM city_en where ProvinceID = ?", [@(provinceId) stringValue]];
        } else {
            resultSet = [db executeQuery:@"SELECT * FROM city where ProvinceID = ?", [@(provinceId) stringValue]];
        }
        while ([resultSet next]) {
            NSString *cityName = [resultSet stringForColumn:@"Name"];
            [cities addObject:cityName];
        }
        
        [db close];
    }
    
    return [cities copy];
}


- (NSArray<NSString *> *)heights {
    if (_heights == nil) {
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSInteger i = 100; i < 230; i++) {
            [arrM addObject:[NSString stringWithFormat:@"%zd cm", i]];
        }
        _heights = arrM.copy;
    }
    
    return _heights;
}

- (NSArray<NSString *> *)weights {
    
    if (_weights == nil) {
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSInteger i = 20; i < 200; i++) {
            
            [arrM addObject:[NSString stringWithFormat:@"%zd kg", i]];
        }
        _weights = arrM.copy;
    }
    
    return _weights;
}

- (NSArray<NSString *> *)tennisExperiences {
    if (_tennisExperiences == nil) {
        NSMutableArray *arrM = [NSMutableArray array];
        [arrM addObject:NSLocalizedString(@"Beginner", @"初学")];
        for (int i = 1; i < 80; i++) {
           
            [arrM addObject: [NSString stringWithFormat:@"%zd %@",i,NSLocalizedString(@"Unit.years", @"年")]];
            
        }
        _tennisExperiences = arrM.copy;
    }
    return _tennisExperiences;
}

- (NSArray<NSString *> *)tennisLevels {
    if (_tennisLevels == nil) {
        NSMutableArray *arrM = [NSMutableArray array];
        for (int i = 2; i < 13; i++) {
            
            float levels = (i / 2.0);
            [arrM addObject:[NSString stringWithFormat:@"%.1f",levels]];
        }
        _tennisLevels = arrM.copy;
        
    }
    return _tennisLevels;
}

- (NSDictionary *)yearAndMonthArr {
    
    if (_yearAndMonthArr == nil) {
        
        NSString *yearMonthString = [[NSDate date] formateYearAndMonth];
        NSInteger lastYear = [[yearMonthString substringToIndex:4] integerValue];
        NSInteger hadmonth = [[yearMonthString substringFromIndex:5] integerValue];
        
        NSArray *normalMonth = [NSArray array];
        if ([NSBundle currentLanguageIsChinese]) {
            normalMonth = @[@"01月",@"02月",@"03月",@"04月",@"05月",@"06月",@"07月",@"08月",@"09月",@"10月",@"11月",@"12月",];
        }else {
            normalMonth = @[@"Jnauary",@"February",@"March", @"April",@"May",@"June", @"July", @"August", @"September", @"October", @"November",@"December"];
        }
        
        NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
        for (NSInteger i = 1970; i <= lastYear; i++) {
            
            if (i != lastYear) {
                
                if ([NSBundle currentLanguageIsChinese]) {
                    [dictM setObject:normalMonth forKey:[NSString stringWithFormat:@"%zd年",i]];
                }else {
                    [dictM setObject:normalMonth forKey:[NSString stringWithFormat:@"%zd",i]];
                }
                
            }else {
                NSMutableArray *monthArrM = [NSMutableArray arrayWithCapacity:hadmonth];
                for (NSInteger i = 0; i < hadmonth; i++) {
                    [monthArrM addObject:normalMonth[i]];
                }
                if ([NSBundle currentLanguageIsChinese]) {
                    [dictM setObject:monthArrM.copy forKey:[NSString stringWithFormat:@"%zd年",i]];
                }else {
                    [dictM setObject:monthArrM.copy forKey:[NSString stringWithFormat:@"%zd",i]];
                }
            }
        }
        
        _yearAndMonthArr = dictM.copy;
        
    }
    return _yearAndMonthArr;
}



- (NSArray<NSString *> *)tennisLineTenssions {
    if (_tennisLineTenssions == nil) {
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSInteger i = 30; i <= 77; i++) {
            [arrM addObject:[NSString stringWithFormat:@"%zd%@", i,NSLocalizedString(@"Unit.lbs", @"磅")]];
        }
        _tennisLineTenssions = arrM.copy;
    }
    return _tennisLineTenssions;
}

- (NSArray<NSString *> *)tennisLineSizes {
    if (_tennisLineSizes == nil) {
        NSString *contentString = @"15,15L,16,16L,17,17L,18,19,1.15mm,1.16mm,1.17mm,1.18mm,1.19mm,1.20mm,1.21mm,1.22mm,1.23mm,1.24mm,1.25mm,1.26mm,1.27mm,1.28mm,1.29mm,1.30mm,1.31mm,1.32mm,1.33mm,1.34mm,1.35mm,1.36mm,1.37mm,1.38mm,1.39mm,1.40mm";
        NSArray *array = [contentString componentsSeparatedByString:@","];
        _tennisLineSizes = [NSMutableArray arrayWithArray:array].copy;
    }
    return _tennisLineSizes;
}

- (NSArray<NSString *> *)tennisLineBrands {
    if (_tennisLineBrands == nil) {
        NSArray *brandAndModelArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TennisLineBrandAndModel.plist" ofType:nil]];
        NSMutableArray *arrM = [NSMutableArray array];
        [brandAndModelArray enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [arrM addObject:obj[@"TennisLineBrand"]];
        }];
        
        _tennisLineBrands = arrM.copy;
    }
    return _tennisLineBrands;
}

@end
